package com.example.widget_sdk;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;


public class SignInView extends View {
    private int width = 0;//控件宽度
    private int height = 0;//控件高度
    private Paint paint;//画笔

    private int lineColor;//横线颜色
    private int textColor;//默认文字颜色
    private int selTextColor;//完成进度文字颜色
    private int topTextColor;//顶部翻倍文字颜色
    private int circleColor;//翻倍以及完成外面环形颜色
    private int normalCircleColor;//默认环颜色

    private int bottomTxtSize;//底部文字字号
    private int topTxtSize;//顶部翻倍字号
    private int contentTxtSize;//中间内容完成字号

    private float radius;
    private Bitmap bitmap;
    //圆中文字
    private String[] contentStrs = {"5", "10", "10", "20", "25", "25", "30"};

    private int currentDay = 0;//签到进度
    //所有圆心位置
    private float[][] centrePoint = new float[7][2];
    private float itemWidth;
    private float itemHeight;
    private float lastX;
    private Canvas canvas;

    public SignInView(Context context) {
        this(context, null);
    }

    public SignInView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SignInView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.signIn, defStyleAttr, R.style.def_sign);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int index = typedArray.getIndex(i);
            if (index == R.styleable.signIn_sign_in_text_color) {

                textColor = typedArray.getColor(index, Color.BLACK);

            } else if (index == R.styleable.signIn_sign_in_line_color) {

                lineColor = typedArray.getColor(index, Color.BLACK);

            } else if (index == R.styleable.signIn_sign_in_top_text_color) {

                topTextColor = typedArray.getColor(index, Color.BLACK);

            } else if (index == R.styleable.signIn_sign_in_normal_circle_color) {

                normalCircleColor = typedArray.getColor(index, Color.BLACK);

            } else if (index == R.styleable.signIn_sign_in_circle_color) {

                circleColor = typedArray.getColor(index, Color.BLACK);

            } else if (index == R.styleable.signIn_sign_in_bottom_text_size) {

                bottomTxtSize = ConvertUtils.dp2px(typedArray.getInt(index, 9));

            } else if (index == R.styleable.signIn_sign_in_top_text_size) {

                topTxtSize = ConvertUtils.dp2px(typedArray.getInt(index, 5));

            } else if (index == R.styleable.signIn_sign_in_center_text_size) {

                contentTxtSize = ConvertUtils.dp2px(typedArray.getInt(index, 14));
            }
        }
        typedArray.recycle();
        selTextColor = Color.parseColor("#ff0000");

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.module_integral_now_checked);
    }

    private Boolean flag = true;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        if (width > 0 && height > 0 && flag) {
            flag = false;
            itemWidth = (float) width / (float) (contentStrs.length * 2 + (contentStrs.length - 1));
            itemHeight = (float) height / 10f;

            if (itemHeight > itemWidth) {
                radius = itemWidth;
            } else {
                radius = (float) height / 4f;
            }
            for (int i = 0; i < contentStrs.length; i++) {
                float cx = (float) (3 * i + 1) * itemWidth;
                float cy = (float) (itemHeight * 4.5);
                centrePoint[i][0] = cx;
                centrePoint[i][1] = cy;
            }

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
        canvas.drawColor(Color.WHITE);
        drawContent(canvas);
        super.onDraw(canvas);

    }

    private void drawContent(Canvas canvas) {
        float cx = 0f;
        float cy = 0f;
        for (int i = 0; i < centrePoint.length; i++) {
            cx = centrePoint[i][0];
            cy = centrePoint[i][1];
            drawCircle(canvas, cx, cy);
            drawCoveringCircle(canvas, cx, cy, radius - ConvertUtils.dp2px(1));
            drawCenterText(canvas, cx, cy, contentStrs[i], i);
            drawBottomText(canvas, cx, cy, i);
            if (i != 0) {
                drawLien(canvas, cx, cy);
            }
            lastX = cx + radius;
        }
    }

    private void drawLien(Canvas canvas, float cx, float cy) {
        paint.reset();
        paint.setColor(lineColor);

        float endX = cx - radius;
        canvas.drawLine(lastX, cy, endX, cy, paint);
    }

    private void drawCenterText(Canvas canvas, float cx, float cy, String text, int i) {
        paint.reset();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(contentTxtSize);
        if (i <= currentDay) {
            Matrix matrix = new Matrix();
            float bitmapWidth = bitmap.getWidth();
            float width = radius * 2;
            float scale = width / bitmapWidth;
            matrix.preScale(scale, scale);
            float dx = cx - radius;
            float dy = cy - radius;
            matrix.postTranslate(dx, dy);
            canvas.drawBitmap(bitmap, matrix, paint);
            return;
        }
        if (i == 3 || i == 6)
            paint.setColor(selTextColor);
        else
            paint.setColor(textColor);
        float y = cy + contentTxtSize / 3;
        canvas.drawText(text, cx, y, paint);

    }

    private void drawCoveringCircle(Canvas canvas, float cx, float cy, float mRadius) {
        paint.reset();
        paint.setColor(circleColor);
        canvas.drawCircle(cx, cy, mRadius, paint);
    }

    private void drawCircle(Canvas canvas, float cx, float cy) {
        paint.reset();
        paint.setColor(normalCircleColor);
        canvas.drawCircle(cx, cy, radius, paint);
    }

    private void drawBottomText(Canvas canvas, float cx, float cy, int i) {
        paint.reset();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(bottomTxtSize);
        String text = "";
        if (i <= currentDay) {
            paint.setColor(selTextColor);
            if (i < currentDay)
                text = (i + 1) + "连签";
            if (i == currentDay)
                text = "今天";
        } else {
            paint.setColor(textColor);
            text = (i + 1) + "连签";
        }

        float y = (float) cy + radius + ConvertUtils.dp2px(10);
        canvas.drawText(text, cx, y, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN) {
            float x = event.getX();
            float y = event.getY();
            for (int i = 0; i < centrePoint.length; i++) {
                float cx = centrePoint[i][0];
                float cy = centrePoint[i][1];
                float v = cx - x;
                float v1 = cy - y;

                double sqrt = Math.sqrt(Math.pow(v, 2) + Math.pow(v1, 2));

                if (sqrt <= radius && i == currentDay + 1) {
                    currentDay +=1;
                    return true;
                }
            }


        }

        return true;

    }
}
